home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / all java / quicktime for java / musicmixer / src / mixer / display / musicpartdisplay.java < prev    next >
Encoding:
Java Source  |  2000-06-23  |  6.0 KB  |  203 lines

  1. /*
  2.  * QuickTime for Java SDK Sample Code
  3.  
  4.    Usage subject to restrictions in SDK License Agreement
  5.  * Copyright: © 1996-1999 Apple Computer, Inc.
  6.  
  7.  */
  8. package mixer.display;
  9.  
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import javax.swing.*;
  13. import javax.swing.event.*;
  14.  
  15. import quicktime.*;
  16. import quicktime.app.audio.*;
  17. import quicktime.std.music.*;
  18. import quicktime.std.*;
  19. import quicktime.std.movies.*;
  20.  
  21. import mixer.mc.*;
  22.  
  23. /** This class is a special display (not extending ChannelDisplay) that provides the
  24.  *  controls specifically suited for a music part.  It also uses a different layout
  25.  *  format than the ChannelDisplay.
  26.  */
  27. public class MusicPartDisplay extends JPanel implements StdQTConstants {
  28.     private MixerComponents[] parts;
  29.     private static final int VOLUME_WIDTH   = 80;
  30.     private static final int VOLUME_HEIGHT  = 20;
  31.     private static final int BALANCE_WIDTH  = 60;
  32.     private static final int BALANCE_HEIGHT = 20;
  33.     
  34.     /** The constructor needs an array of MixerMusicPart objects that it is creating the 
  35.      *  control panel for.  It will create a mixer for each entry in the array.
  36.      *  @param the MixerComponents [] to make the display for
  37.      */
  38.     public MusicPartDisplay (Movie m, MixerComponents[] musicParts) throws QTException {
  39.         super();
  40.         parts = musicParts;
  41.  
  42.         setLayout(new GridBagLayout());
  43.         buildMusicControl(m);
  44.     }
  45.     
  46.     /* This method goes through the work of creating a control panel entry for each
  47.      * channel.
  48.      */
  49.     private void buildMusicControl (Movie m) throws QTException {
  50.         int numParts = parts.length;
  51.         GridBagConstraints cons = new GridBagConstraints();
  52.         cons.gridx = GridBagConstraints.RELATIVE;
  53.         cons.gridy = 0;
  54.         cons.gridwidth = 1;
  55.         cons.gridheight = 1;
  56.         cons.weightx = 0.0F;
  57.         cons.weighty = 0.0F;
  58.         
  59.         add(new Label("Instrument"), cons);
  60.         add(new Label("Volume"), cons);
  61.         add(new Label("Balance"), cons);
  62.         add(new Label("Mute"), cons);
  63.         
  64.         for (int i = 0; i < numParts; i++) {
  65.             cons.gridy = i + 1;
  66.             cons.fill = GridBagConstraints.HORIZONTAL;
  67.             
  68.             MusicPart mpc = (MusicPart)parts[i].getMaster();
  69.             addInstrumentButton (m, cons, mpc);
  70.             
  71.             cons.fill = GridBagConstraints.NONE;
  72.             addVolumeSlider (cons, mpc);
  73.             addBalanceSlider (cons, mpc);
  74.             addMuteButton (cons, mpc);
  75.         }
  76.     }
  77.     
  78.     /* This is what has to happen to add an Instrument Choosing button for a channel. */
  79.     private void addInstrumentButton (Movie m, Object cons, MusicPart c) throws QTException{
  80.         JButton instrument = new JButton(c.getInstrumentName());
  81.         add(instrument, cons);
  82.         
  83.         InstrumentListener l = new InstrumentListener(m, c);
  84.         instrument.addActionListener(l);
  85.     }
  86.     
  87.     /* This is what has to happen to add a Volume Slider for a channel. */
  88.     private void addVolumeSlider (Object cons, MusicPart c) throws QTException {
  89.         JSlider volume = new JSlider(JSlider.HORIZONTAL, 0, 128, 100);
  90.         volume.setPreferredSize(new Dimension(VOLUME_WIDTH, VOLUME_HEIGHT));
  91.         volume.setMinimumSize(new Dimension(VOLUME_WIDTH, VOLUME_HEIGHT));
  92.         add (volume, cons);
  93.  
  94.         VolumeListener l = new VolumeListener(c);
  95.         volume.addChangeListener(l);
  96.         volume.setValue((int)(128.0F * c.getVolume()));
  97.     }
  98.     
  99.     /* This is what has to happen to add a Balance Slider for a channel. */
  100.     private void addBalanceSlider (Object cons, MusicPart c) throws QTException {
  101.         JSlider balance = new JSlider(JSlider.HORIZONTAL, -127, 127, 0);
  102.         balance.setPreferredSize(new Dimension(BALANCE_WIDTH, BALANCE_HEIGHT));
  103.         balance.setMinimumSize(new Dimension(BALANCE_WIDTH, BALANCE_HEIGHT));
  104.         add(balance, cons);
  105.  
  106.         BalanceListener l = new BalanceListener(c);
  107.         balance.addChangeListener(l);
  108.         balance.setValue((int)(127.0F * c.getBalance()));
  109.     }
  110.     
  111.     /* This is what has to happen to add a Mute Button for a channel. */
  112.     private void addMuteButton (Object cons, MusicPart c) throws QTException {
  113.         JCheckBox mute = new JCheckBox();
  114.         add(mute, cons);
  115.         
  116.         MuteListener l = new MuteListener(c);
  117.         mute.addActionListener(l);
  118.         mute.setSelected(c.isMuted());
  119.     }
  120.     
  121.     /* The following inner classes are the listeners for the instrument button, volume
  122.      * slider, and balance slider.  Notice that each one takes a MusicPart object
  123.      * in the constructor so that it know which channel it's acting upon.
  124.      */
  125.     class InstrumentListener implements ActionListener {
  126.         private MusicPart part;
  127.         private Movie mov;
  128.         
  129.         public InstrumentListener (Movie m, MusicPart c) {
  130.             part = c;
  131.             mov = m;
  132.         }
  133.         
  134.         public void actionPerformed(ActionEvent e) {
  135.             try {
  136.                 if (mov != null) {
  137.                     part.getNoteChannel().pickEditInstrument("Choose instrument...", kPickEditAllowPick);
  138.                 } else
  139.                     part.selectInstrument("Choose instrument...");
  140.                 ((JButton) e.getSource()).setText(part.getInstrumentName());
  141.             }
  142.             catch (QTException ex) {
  143.                 if (-128 != ex.errorCode()) { // deals with the "user cancelled" case
  144.                     ex.printStackTrace();
  145.                 }
  146.             }
  147.         }
  148.     }
  149.         
  150.     class VolumeListener implements ChangeListener {
  151.         private MusicPart part;
  152.         
  153.         public VolumeListener (MusicPart c) {
  154.             part = c;
  155.         }
  156.         
  157.         public void stateChanged(ChangeEvent e) {
  158.             try {
  159.                 Integer tmp = new Integer(((JSlider) e.getSource()).getValue());
  160.                 part.setVolume (tmp.floatValue() / 128.0F);
  161.             }
  162.             catch (QTException ex) {
  163.                 ex.printStackTrace();
  164.             }
  165.         }
  166.     }
  167.     
  168.     class BalanceListener implements ChangeListener {
  169.         private MusicPart part;
  170.         
  171.         public BalanceListener(MusicPart c) {
  172.             part = c;
  173.         }
  174.         
  175.         public void stateChanged(ChangeEvent e) {
  176.             try {
  177.                 Integer b = new Integer(((JSlider) e.getSource()).getValue());
  178.                 part.setBalance (b.floatValue() / 127.0F);
  179.             }
  180.             catch (QTException ex) {
  181.                 ex.printStackTrace();
  182.             }
  183.         }
  184.     }
  185.     
  186.     class MuteListener implements ActionListener {
  187.         private MusicPart part;
  188.         
  189.         public MuteListener(MusicPart c) {
  190.             part = c;
  191.         }
  192.         
  193.         public void actionPerformed(ActionEvent e) {
  194.             try {
  195.                 JCheckBox source = (JCheckBox) e.getSource();
  196.                 part.setMuted(source.isSelected());
  197.             }
  198.             catch (QTException ex) {
  199.                 ex.printStackTrace();
  200.             }
  201.         }
  202.     }
  203. }